﻿package de.gameduell.framework.facebook.app.pages {
	import de.gameduell.facebook.service.gen.UserProfile;
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.facebook.FBBaseController;
	import de.gameduell.framework.facebook.FBBasePageManager;
	import de.gameduell.framework.facebook.Page;
	import de.gameduell.framework.facebook.communication.FacebookData;
	import de.gameduell.framework.gui.button.Button;
	import de.gameduell.framework.gui.button.ButtonEvent;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.ColorTransform;
	import flash.utils.Timer;

	public class BuddyList extends Page {
		
		public static const INIT_DONE:String = "buddylist_done";
		private static const MAX_FRIENDS:int = 8;
		
		private var currentIndex:int = 0;
		private var stepLeft:Button;
		private var jumpLeft:Button;
		private var stepRight:Button;
		private var jumpRight:Button;
		
		private var buddyItems:Array;
		private var itemContainer:Sprite;
		private var busyAnimation:Sprite;

		public function BuddyList(aPageManager:FBBasePageManager, anId:String) {
			super(aPageManager, anId);
		}

		override protected function initPage():void {
			itemContainer = page.getChildByName("itemContainer") as Sprite;
			busyAnimation = page.getChildByName("busyAnimation") as Sprite;
			stepLeft = new Button(page.getChildByName("stepLeft") as Sprite);
			jumpLeft = new Button(page.getChildByName("jumpLeft") as Sprite);
			stepRight = new Button(page.getChildByName("stepRight") as Sprite);
			jumpRight = new Button(page.getChildByName("jumpRight") as Sprite);
			
			controlsActive = false;
			
			stepLeft.addEventListener(ButtonEvent.CLICK, onStepLeft);		
			jumpLeft.addEventListener(ButtonEvent.CLICK, onJumpLeft);		
			stepRight.addEventListener(ButtonEvent.CLICK, onStepRight);		
			jumpRight.addEventListener(ButtonEvent.CLICK, onJumpRight);

			controller.facebookData.addEventListener(FacebookData.FBE_FRIEND_DATA, update);
			controller.addEventListener(FBBaseController.NEW_HIGHSCORE, update);
			update();
		}
		
		private function set controlsActive(isActive:Boolean):void {
			busyAnimation.visible = !isActive;
			stepLeft.active = isActive;
			stepLeft.sprite.alpha = isActive ? 1 : 0.25;
			jumpLeft.active = isActive;
			jumpLeft.sprite.alpha = isActive ? 1 : 0.25;
			stepRight.active = isActive;
			stepRight.sprite.alpha = isActive ? 1 : 0.25;
			jumpRight.active = isActive;
			jumpRight.sprite.alpha = isActive ? 1 : 0.25;
		}
		
		private function update(event:Event = null):void {
			try{
				if(buddyItems != null){
					for(var n:int = 0; n < itemContainer.numChildren - 1; n++){
						itemContainer.removeChildAt(n);
					}
					buddyItems.sort(sortByHighScore);
					for(var i:int = 0; i < buddyItems.length; i++){
						var addedItem:Sprite = Sprite(itemContainer.addChild(BuddyListItem(buddyItems[i]).sprite));
						addedItem.x = (i + 1) * -BuddyListItem.ITEM_WIDTH;
					}
					currentIndex = buddyItems.length > MAX_FRIENDS ? MAX_FRIENDS : buddyItems.length;
				}else{
					buildBuddyList();
				}
			}catch(e:Error){
				Debug.trace("BuddyList::update() - FATAL ERROR - report this stuff ...", Debug.WARNING);
				// TODO - Error reporting ...
			}
		}
		
		public function buildBuddyList(event:Event = null):void {
			if(controller.facebookData.friendProfiles != null){
				controller.facebookData.removeEventListener(FacebookData.FBE_FRIEND_DATA, update);
				buddyItems = new Array();
				for each(var aBuddy:UserProfile in controller.facebookData.friendProfiles){
					if(aBuddy.highscore == "0" && aBuddy != controller.facebookData.user) {
						buddyItems.push(new InviteBuddyListItem());
					}else{
						var listItem:BuddyListItem = new BuddyListItem(aBuddy);
						buddyItems.push(listItem);
					}
				}
				buddyItems.sort(sortByHighScore);
				
				if(controller.facebookData.friendProfiles.length < MAX_FRIENDS){
					var diff:int = MAX_FRIENDS - controller.facebookData.friendProfiles.length;
					for(var n:int = 0; n < diff; n++){
						buddyItems.push(new InviteBuddyListItem());
					}
				}
				
				for(var i:int = 0; i < buddyItems.length; i++){
					var addedItem:Sprite = Sprite(itemContainer.addChild(BuddyListItem(buddyItems[i]).sprite));
					addedItem.x = (i + 1) * -BuddyListItem.ITEM_WIDTH;
				}
				currentIndex = buddyItems.length > MAX_FRIENDS ? MAX_FRIENDS : buddyItems.length;
				controlsActive = true;
			}else{
				Debug.trace("BuddyList::buildBuddyList: friendProfiles empty - waiting for data event ...", Debug.DEBUG);
			}
		}
		
		private function sortByHighScore(a:BuddyListItem, b:BuddyListItem):int {
			if(int(a.buddy.highscore) < int(b.buddy.highscore)){
				return 1;
			} else if(int(a.buddy.highscore) > int(b.buddy.highscore)){
				return -1;
			} else {
				return 0;
			}
		}

		private function onStepLeft(e:ButtonEvent = null):void {
			if((currentIndex + 1) <= buddyItems.length){
				itemContainer.x += BuddyListItem.ITEM_WIDTH;
				currentIndex++;
			}else{
				flash();
			}
		}

		private function onStepRight(e:ButtonEvent = null):void {
			if((currentIndex - 1) >= MAX_FRIENDS) {
				itemContainer.x -= BuddyListItem.ITEM_WIDTH;
				currentIndex--;
			}else{
				flash();
			}
		}

		private function onJumpLeft(e:ButtonEvent):void {
			for(var i:int = 0; i < MAX_FRIENDS; i++){
				onStepLeft();
			}
		}

		private function onJumpRight(e:ButtonEvent):void {
			for(var i:int = 0; i < MAX_FRIENDS; i++){
				onStepRight();
			}
		}
		
		private function flash():void {
			page.transform.colorTransform = new ColorTransform(1.1,1.1,1.1);
			var flashTimer:Timer = new Timer(80, 1);
			flashTimer.addEventListener(TimerEvent.TIMER_COMPLETE, restoreColors);
			flashTimer.start();
		}
		
		private function restoreColors(event:Event = null):void {
			page.transform.colorTransform = new ColorTransform();
		}
	}
}
